from django.shortcuts import render,HttpResponse,redirect
from .models import Student,Question_bank
# Create your views here.
def student(request):
    if request.method=="GET":
        return render(request,'add.html')
    elif request.method=='POST':
        name=request.POST.get('name')
        classname=request.POST.get('classname')
        cardid=request.POST.get('cardid')
        phone=request.POST.get('phone')
        student1=Student(name=name,classname=classname,cardid=cardid,phone=phone)
        student1.save()
        return HttpResponse(f'姓名:{name},班级名:{classname},身份证号:{cardid},手机号{phone}')


def question_bank(request):
    if request.method=='GET':
        return render(request,'question_bank.html')
    elif request.method=="POST":
        question_type=request.POST.get('question_type')#题目类型
        headline=request.POST.get('headline')#标题
        describe=request.POST.get('describe')#描述
        title_tag=request.POST.get('title_tag')#题目标签
        question_steam=request.POST.get('question_steam')#题干
        reference_version=request.POST.get('reference_version')#参考答案
        difficulty_level=request.POST.get('difficulty_level')#难易度
        qb=Question_bank(question_type=question_type,headline=headline,describe=describe,title_tag=title_tag,
                         question_steam=question_steam,reference_version=reference_version,difficulty_level=difficulty_level)
        qb.save()
        return HttpResponse(f'题目类型 :{question_type},标题:{headline},描述:{describe},题目标签:{title_tag},题干:{question_steam}'
                            f'参考答案:{reference_version},难易度:{difficulty_level}')
def select(request):
    #获取全部
    Access_to_all=Student.objects.all()

    return render(request,'select.html',{'Access_to_all':Access_to_all})

    #获取单个值
    # ata=Student.objects.get(name='路飞')
    # print(ata.name,ata.phone,ata.cardid,ata.classname)
    # return HttpResponse('hhd')
# 更改
def change(request):
    # update=Student.objects.filter(name='乔巴').update(name='乌索普')
    # return redirect(select)

    # upd=Student.objects.get(id=4)
    # upd.name='胡汉三'
    # upd.cardid='asdas'
    # upd.phone='dsadas'
    # upd.save()
    # return redirect(select)

    # 如果没有要查找的数据就添加一条新的数据
    # up=Student.objects.filter(name='大将黄猿').get_or_create(name='黄猿',classname='海军',cardid='闪光踢',phone='大萨达奥所大所大')
    # return redirect(select)

#     如果有数据就更改没有数据就创建一条新数据
    up=Student.objects.filter(name='大将青雉').update_or_create(name='青雉',classname='海军',cardid='冰河世纪',phone='大萨达奥所大所大')
    return redirect(select)
# 删除
def deletes(request):
    Student.objects.filter(name='胡汉三').delete()
    return redirect(select)







